package com.phb.puhuibao.web.controller;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.idp.pub.entity.Pager;
import com.idp.pub.service.IBaseService;
import com.idp.pub.web.controller.BaseController;
import com.phb.puhuibao.entity.CustomerManagement;
import com.phb.puhuibao.entity.Evaluate;
 

@Controller
@RequestMapping(value = "/customerManagement")
public class CustomerManagementController extends BaseController<CustomerManagement, String> {
	@Resource(name = "customerManagementService")
	public void setBaseService(IBaseService<CustomerManagement, String> baseService) {
		super.setBaseService(baseService);
	}
	
	@javax.annotation.Resource(name = "jdbcTemplate")
	private JdbcTemplate jdbcTemplate;
	
	/**
	 * 本期总额
	 * @param id
	 * @return
	 */
	@RequestMapping(value="getSum")
	@ResponseBody
	public  Map<String, Object> getSum(){
		Map<String, Object> data = new HashMap<String, Object>();
		List<Map<String, Object>> getsum = null;

		 try {

				
				Date date = new Date();
				 SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd");
				 SimpleDateFormat df1=new SimpleDateFormat("yyyy-MM");
				 
				 String nowdate= df.format(date); 
				 String nowmonth= df1.format(date); 
			     String lastmonth =   df1.format(getLastDate(date)) ;
			     
			     String last25 = lastmonth+"-25";
				 String this10 = nowmonth+"-10";
				 String this25 = nowmonth+"-25";
				 
			 Date todaydate =  df.parse(nowdate);
			 Date last25d =    df.parse(last25);
			 Date this10d =    df.parse(this10);
			 Date this25d =    df.parse(this25);
			 
			 String sql ="";
			 String countsql="";
			 if(todaydate.after(this10d) && this25d.after(todaydate) || todaydate.equals(this25d)){//如果现在的时间是在这个月的10号和25号之间,包含15号
				 sql= " select    sum(contract_amount) sum   from crm_contract  where  contract_startdate > str_to_date("+ this10 +",'%Y-%m-%d') and contract_startdate <= str_to_date("+ this25 +",'%Y-%m-%d') ";
				 countsql= " select    count(contract_amount) count   from crm_contract  where  contract_startdate > str_to_date("+ this10 +",'%Y-%m-%d') and contract_startdate <= str_to_date("+ this25 +",'%Y-%m-%d') ";
	         }else  if(todaydate.after(last25d) && this10d.after(todaydate) || todaydate.equals(this10d)){// 如果现在是本月10号（包含）和上月25号之间 
	        	 
	        	 sql= " select    sum(contract_amount) sum   from crm_contract  where  contract_startdate > str_to_date("+ last25 +",'%Y-%m-%d') and contract_startdate <= str_to_date("+ this10 +",'%Y-%m-%d') ";
	        	 countsql ="select    count(contract_amount) count   from crm_contract  where  contract_startdate > str_to_date("+ last25 +",'%Y-%m-%d') and contract_startdate <= str_to_date("+ this10 +",'%Y-%m-%d') ";
	         }
			 
			 String sumstring ="0.0";
			 String countstring ="0";
			 
				getsum = this.jdbcTemplate.queryForList(sql);
				
				if(getsum.get(0) .get("sum") != null){
					  sumstring =   getsum.get(0) .get("sum").toString();
				}
				
				getsum = this.jdbcTemplate.queryForList(countsql);
			 
				if(getsum.get(0) .get("count") != null){
					countstring =   getsum.get(0) .get("count").toString();
				}
				
				data.put("sumstring", sumstring);
				data.put("countstring", countstring);
					 
				data.put("status", 1);
				return data;	
		} catch (ParseException e) {
			 
			data.put("result", "获取本期借款人总额失败，请刷新页面！");
			data.put("status", 0);
			return data;
		}

		
		
 

 
	 
	 	
	}
	
	private  Date getLastDate(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.MONTH, -1);
        return cal.getTime();
    }

}
